<?php

function sc_software_list() {
  $header = array(
      t('Phần mềm'),
      t('Alias'),
      t('Ngày tạo'),
      t('User'),
      array('data' => t('Operations'), 'colspan' => 3),
  );

  $sc_softwares = db_select('sc_software', 'e')
          ->addTag('translatable')
          ->fields('e')
          ->orderBy('e.date_create', 'DESC')
          ->execute()
          ->fetchAll();

  $rows = array();
  if ($sc_softwares) {
    foreach ($sc_softwares as $sc_software) {
      $rows[] = array(
          check_plain($sc_software->scs_name),
          check_plain($sc_software->scs_alias),
          check_plain($sc_software->date_create),
          check_plain($sc_software->user_id),
          l(t('Chi tiết'), 'admin/structure/sckeys/detail/' . $sc_software->scs_id),
          l(t('Sửa'), 'admin/structure/sckeys/edit/' . $sc_software->scs_id),
          l(t('Xóa'), 'admin/structure/sckeys/delete/' . $sc_software->scs_id),
      );
    }
  }

  if (!$rows) {
    $rows[] = array(array(
            'data' => t('Không có liên hệ nào được gửi đến bạn'),
            'colspan' => 8,
    ));
  }
  $build['send_form_table'] = array(
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $rows,
  );
  return $build;
}

function sc_software_add($form, &$form_state, $id = 0) {

  if ($id) {
    $scSoftware = sc_software_dbget($id);
    drupal_set_title($scSoftware->name);
  }

  $form['scs_name'] = array(
      '#type' => 'textfield',
      '#title' => t('Tên'),
      '#required' => TRUE,
      '#value' => isset($scSoftware) ? $scSoftware->name : null,
      '#description' => t("Tên của phần mềm."),
  );
  $form['scs_alias'] = array(
      '#type' => 'textfield',
      '#required' => TRUE,
      '#value' => isset($scSoftware) ? $scSoftware->alias : null,
      '#title' => t('Alias:'),
  );
  $form['scs_id'] = array(
      '#type' => 'hidden',
      '#value' => isset($scSoftware) ? $scSoftware->pid : null,
  );


  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => 'Save',
  );

  return $form;
}

function sc_software_add_validate($form, &$form_state) {
  if (empty($form_state['input']['scs_name'])) {
    form_set_error('', t('Tên không được để trống.'));
  }

  if (empty($form_state['input']['scs_alias'])) {
    form_set_error('', t('Alias không được để trống.'));
  }
}

function sc_software_add_submit($form, &$form_state) {
  global $user;
  $form_state['input']['date_create'] = date('Y-m-d H:i:s', time());
  $form_state['input']['user_id'] = $user->uid;
  
  if (!empty($form_state['input']['scs_id'])) {
    drupal_write_record('sc_software', $values, array($form_state['input']['scs_id']));
  } else {
    $form_state['input']['scs_id'] = 0;
    drupal_write_record('sc_software', $form_state['input']);
  }

  drupal_goto('admin/structure/sckeys');
}

function sc_software_delete() {
  
}

function sc_software_dbupdate($values) {
  global $uid;
  $query = db_update('sc_software')
          ->fields(array(
              'name' => $values['name'],
              'alias' => $values['alias'],
              'user_id' => $uid,
          ))
          ->condition('scs_id', $values['id'])
          ->execute();

  return $query;
}

function sc_software_dbinsert($values) {
  $query = db_insert('sc_software')
          ->fields(array(
              'scs_id' => 1,
              'name' => 'ten',
              'alias' => 'khac',
              'create' => $values['create'],
              'user_id' => 1,
          ))
          ->execute();
  return $query;
}

function sc_software_dbget($id) {
  return db_select('sc_software', 'p')
                  ->fields('p')->condition('scs_id', $id)
                  ->execute()->fetch();
}

?>